
package com.auto.ding.util;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

/**
 * 辅助功能API
 */
public class AcessibilityApi {
    public enum ActionType {
        BACK,  //返回键
        HOME,  //home
        SETTING,  //设置
        POWER,  //锁屏
        RECENTS,  //应用列表
        NOTIFICATIONS, //通知
        SCROLL_BACKWARD,  //下滑
        SCROLL_FORWARD, //上划

    }

    private static AccessibilityEvent mAccessibilityEvent = null;
    private static AccessibilityService mAccessibilityService = null;

    /**
     * 设置数据
     */
    public static void setAccessibilityService(AccessibilityService service) {
        synchronized (AcessibilityApi.class) {
            if (service != null && mAccessibilityService == null) {
                mAccessibilityService = service;
            }

        }

    }

    public static void setAccessibilityEvent(AccessibilityEvent event) {
        synchronized (AcessibilityApi.class) {
            if (event != null && mAccessibilityEvent == null) {
                mAccessibilityEvent = event;
            }
        }
    }

    /**
     * 模拟点击系统相关操作
     */
    public static void performAction(ActionType action) {
        if (mAccessibilityService == null) {
            return;
        }

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        switch (action) {
            case BACK:
                mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);

                break;
            case HOME:
                mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);

                break;
            case RECENTS:
                mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);

                break;
            case NOTIFICATIONS:
                mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS);

                break;

            case POWER:
                mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_POWER_DIALOG);

                break;

            case SETTING:
                mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS);


                break;
            case SCROLL_BACKWARD:
                mAccessibilityService.performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);


                break;
            case SCROLL_FORWARD:
                mAccessibilityService.performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);


                break;
        }
    }

}
